package com.dengke.hellospringmvc.dao;

import java.sql.Types;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class QueryForListReturnListMapDAO extends JdbcDaoSupport {
	@Autowired
	public QueryForListReturnListMapDAO(DataSource dataSource) {
		this.setDataSource(dataSource);
	}

	public List<Map<String, Object>> queryForList_ListMap() {
		String sql = "select Id,Email from Address";
		List<Map<String, Object>> list = this.getJdbcTemplate().queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> queryForList_ListMap2() {
		String sql = "select Id,Email from Address " + "where CreatedOnUtc>?";
		Date createdTime=getDateByYear(1981);
		Object[]args=new Object[]{createdTime,2800};
		int[]argType=new int[]{java.sql.Types.DATE,Types.DOUBLE};
		List<Map<String, Object>> list = this.getJdbcTemplate().queryForList(sql);
		return list;
	}
	
	private Date getDateByYear(int year) {
		java.sql.Date date=new java.sql.Date(System.currentTimeMillis());
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, Calendar.JANUARY);
		calendar.set(Calendar.DAY_OF_YEAR, 1);
		return calendar.getTime();
	}
}
